package com.hj.saapp.common.utils;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import com.hj.saapp.BCDApplication;

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public class DeviceUtils {


    public static float displayDensity = 0.0F;


    public DeviceUtils() {
    }

    public static float dpToPixel(float dp) {
        return dp * (getDisplayMetrics().densityDpi / 160F);
    }


    public static float getDensity() {
        if (displayDensity == 0.0)
            displayDensity = getDisplayMetrics().density;
        return displayDensity;
    }

    public static DisplayMetrics getDisplayMetrics() {
        DisplayMetrics displaymetrics = new DisplayMetrics();
        ((WindowManager) BCDApplication.getApp().getSystemService(
                Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(
                displaymetrics);
        return displaymetrics;
    }

    public static float getScreenHeight() {
        return getDisplayMetrics().heightPixels;
    }

    public static float getScreenWidth() {
        return getDisplayMetrics().widthPixels;
    }


    public static float pixelsToDp(float f) {
        return f / (getDisplayMetrics().densityDpi / 160F);
    }

    public static int getVersionCode() {
        int versionCode = 0;
        try {
            versionCode = BCDApplication.getApp()
                    .getPackageManager()
                    .getPackageInfo(BCDApplication.getApp().getPackageName(),
                            0).versionCode;
        } catch (NameNotFoundException ex) {
            versionCode = 0;
        }
        return versionCode;
    }

    public static int getVersionCode(String packageName) {
        int versionCode = 0;
        try {
            versionCode = BCDApplication.getApp().getPackageManager()
                    .getPackageInfo(packageName, 0).versionCode;
        } catch (NameNotFoundException ex) {
            versionCode = 0;
        }
        return versionCode;
    }

    public static String getVersionName() {
        String name = "";
        try {
            name = BCDApplication.getApp()
                    .getPackageManager()
                    .getPackageInfo(BCDApplication.getApp().getPackageName(),
                            0).versionName;
        } catch (NameNotFoundException ex) {
            name = "";
        }
        return name;
    }


    public static String getIMEI() {
        TelephonyManager tel = (TelephonyManager) BCDApplication.getApp()
                .getSystemService(Context.TELEPHONY_SERVICE);
        String ime = tel.getDeviceId();
        if (ime == null)
        {
            ime = tel.getSubscriberId();
        }

        return ime;
    }

    public static String getPhoneType() {
        return "android";
    }


}
